package com.cgbl.service.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.AddTeamMemberAdapter
import com.cgbl.service.adapter.AddTeamTagAdapter
import com.cgbl.service.databinding.AppActivityMemberSearchBinding
import com.cgbl.service.databinding.AppActivityTeamManagerBinding
import com.cgbl.service.pop.CommonConfirmPop
import com.cgbl.service.utils.city.BeanTransferHelper
import com.cgbl.service.vm.MemberSearchViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean
import com.robot.base.model.UserInfoBean
import com.robot.base.util.arouter.ARouterUtil

@Route(path = AppConstants.Router.Project.A_MEMBER_SEARCH)
class MemberSearchActivity :
    GLBaseActivity<AppActivityMemberSearchBinding, MemberSearchViewModel>() {

    lateinit var addTeaMemberAdapter: AddTeamMemberAdapter

    var keyword = ""

    private var selectType = AppConstants.BundleValue.SELECT_MEMBER_TYPE_MANAGER

    private var selectMember: MemberAdapterBean? = null

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_member_search

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        addTeaMemberAdapter = AddTeamMemberAdapter(false)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = addTeaMemberAdapter
        }
        addTeaMemberAdapter.setOnItemClickListener { adapter, view, position ->
            selectMember = addTeaMemberAdapter.data[position]
            if (selectType == AppConstants.BundleValue.SELECT_MEMBER_TYPE_FRAME) {
                //人员架构设置
                ARouterUtil.goToActivityResult(
                    AppConstants.Router.Project.A_SET_REWARD,
                    this@MemberSearchActivity,
                    AppConstants.ActivityRequestCode.SET_WARD_RESULT
                )
                return@setOnItemClickListener
            }
            val commonConfirmPop = CommonConfirmPop(this@MemberSearchActivity)
            XPopup.Builder(this@MemberSearchActivity).asCustom(commonConfirmPop).show()
            commonConfirmPop.setTitle("提示").setLeftText("取消").setRightText("添加")
                .setDesc("是否添加该人员?").setRightClickListener {
                    viewModel.addMember(addTeaMemberAdapter.data[position].accountId ?: "")
                }
        }
        viewModel.fetchMemberList(keyword)
        binding.tvSearchTitle.text = "“${keyword}”搜索结果"
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        val rightView = findViewById<TextView>(R.id.tv_right_text)
        rightView?.visibility = View.VISIBLE
        titleView?.text = "添加人员"
        backView?.setOnClickListener { finish() }
    }

    override fun initViewObservable() {
        viewModel.uc.getUserInfoDataEvent.observe(this) {
            addTeaMemberAdapter.setNewInstance(BeanTransferHelper.userInfoTransferMemberAdapter(it))
        }
    }

    override fun initParam() {
        keyword = intent.getStringExtra(AppConstants.BundleKey.KEYWORD) ?: ""
        selectType = intent.getIntExtra(
            AppConstants.BundleKey.SELECT_MEMBER_TYPE,
            AppConstants.BundleValue.SELECT_MEMBER_TYPE_MANAGER
        )
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AppConstants.ActivityRequestCode.SET_WARD_RESULT) {
            if (resultCode == RESULT_OK) {
                val reward = data?.getStringExtra(AppConstants.BundleKey.REWARD_MONEY)
                selectMember?.wages = reward
                ARouterUtil.goToActivityWithBundle(
                    AppConstants.Router.Project.A_MEMBER_FRAME_PROFILE,
                    Bundle().apply {
                        putSerializable(AppConstants.BundleKey.MEMBER_BEAN, selectMember)
                    })
                finish()
            }
        }
    }
}